package com.zhoufy.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-04-14.
 *
 * @author choufy
 */
public class UserClientHandle extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext context;
    private Object result;
    private Object para;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 收到服务端数据 唤醒等待线程
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        result = msg;
        System.out.println("channelRead method" + msg);
        notify();
    }

    /**
     *  写出数据 开始等待唤醒
     */
    @Override
    public synchronized Object call() throws InterruptedException {
        context.writeAndFlush(para);
        System.out.println("call");
        wait();
        System.out.println("wait stop");
        return result;
    }

    void setPara(Object para) {
        this.para = para;
    }
}
